// pages/musicPlay/musicPlay.js
const app = getApp().globalData
const Lyric = require('../../utils/lyric.js')
const util = require('../../utils/util.js')

function getLyric(songmid) {
  return new Promise((resolve) => {
    wx.request({
      url: `https://api.darlin.me/music/lyric/${songmid}`,
      method: 'GET',
      success: (res) => {
        resolve(res)
      }
    })
  })
}
const SEQ_MODE = 1
const RANDOM_MOD = 2
const SINGLE_CYCLE_MODE = 3
Page({
  /**
   * 页面的初始数据
   */
  data: {
    alltime:0,
    playurl: '',
    playIcon: 'icon-play',
    cdCls: 'pause',
    currentLyric: null,
    currentLineNum: 0,
    toLineNum: -1,
    currentSong: null,
    dotsArray: new Array(2),
    currentDot: 0,
    playMod: SEQ_MODE,
    uid: '',
    duration:'',
    width:'',
  },
  onShow: function () {
    this._init()
    this.setData({
      uid: this.getUid()
    })
  },
 //初始化
 _init: function () {
  let songslist = (app.songlist.length && app.songlist) || wx.getStorageSync('songlist')
  let currentSong = app.songlist[app.currentIndex] || (songslist && songslist[app.currentIndex])
  let duration = currentSong && currentSong.duration

  this.setData({
    alltime:duration,
    currentSong: currentSong,
    duration: this._formatTime(duration),
    songslist: songslist,
    currentIndex: app.currentIndex
  })

  this._getPlayUrl(currentSong.mid)
  this._getLyric(currentSong)
},
  // 获取背景播放音乐的id
  _getBackPlayfileName: function() {
    return new Promise((resolve, reject) => {
        wx.getBackgroundAudioPlayerState({
          success: (result) => {
            let dataUrl = result.dataUrl
            let ret = dataUrl && dataUrl.split('?')[0].split('/')[3]
            resolve({ret, result})
          },
          fail(e) {
            let ret = false
            reject(ret)
          }
        })
    })
  },
  // 更新进度
  updateProgress(percent) {
    const barWidth = this.data.barWidth
    this.setData({
      width: barWidth * percent
    })
  },
  // 获取播放地址
  _getPlayUrl: function (songmidid) {
    const _this = this
    wx.request({
      url: `https://u.y.qq.com/cgi-bin/musicu.fcg?format=json&data=%7B%22req_0%22%3A%7B%22module%22%3A%22vkey.GetVkeyServer%22%2C%22method%22%3A%22CgiGetVkey%22%2C%22param%22%3A%7B%22guid%22%3A%22358840384%22%2C%22songmid%22%3A%5B%22${songmidid}%22%5D%2C%22songtype%22%3A%5B0%5D%2C%22uin%22%3A%221443481947%22%2C%22loginflag%22%3A1%2C%22platform%22%3A%2220%22%7D%7D%2C%22comm%22%3A%7B%22uin%22%3A%2218585073516%22%2C%22format%22%3A%22json%22%2C%22ct%22%3A24%2C%22cv%22%3A0%7D%7D`,
      method: 'get',
      success: function (res,statusCode,header,cookies,profile) {
        if (res.data.req_0.data.midurlinfo[0].purl) {
          _this._createAudio(res.data.req_0.data.sip[0] + res.data.req_0.data.midurlinfo[0].purl)
        } else {
          wx.showToast({
            title: '本歌曲暂时不能播放。请切换下一首',
            icon: 'none'
          })
          _this._createAudio(res.data.req_0.data.sip[0] + res.data.req_0.data.midurlinfo[0].filename)
        }
      }
    })
  },
  getUid () {
    let _uid = JSON.parse(JSON.stringify(this.data.uid))
    if (_uid) {
      return _uid
    }
    if (!_uid) {
      const t = (new Date()).getUTCMilliseconds()
      _uid = '' + Math.round(2147483647 * Math.random()) * t % 1e10
    }
    return _uid
  },


  // 创建播放器
  _createAudio: function (playUrl) {
    console.log(playUrl,111);
    wx.playBackgroundAudio({
      dataUrl: playUrl,
      title: this.data.currentSong.name,
      coverImgUrl: this.data.currentSong.image
    })
    // 监听音乐播放
    wx.onBackgroundAudioPlay((res) => {
      this.setData({
        playIcon: 'icon-pause',
        cdCls: 'play'
      })
    })
    // 监听音乐暂停
    wx.onBackgroundAudioPause((res) => {
      this.setData({
        playIcon: 'icon-play',
        cdCls: 'pause'
      })
    })
    // 监听音乐停止
    wx.onBackgroundAudioStop((res) => {
      if (this.data.playMode === SINGLE_CYCLE_MODE) {
        this._init()
        return
      }
      this.next()
    })
    // 监听播放器播放进度
    const manage = wx.getBackgroundAudioManager()
    manage.onTimeUpdate(() => {
      const currentTime = manage.currentTime
      this.setData({
        currentTime: this._formatTime(currentTime),
        percent: currentTime / this.data.currentSong.duration,
        width: currentTime / this.data.alltime * 100
      })
      if (this.data.currentLyric) {
        this.handleLyric(currentTime * 1000)
      }
    })
  },
  // 切换播放模式
  changeMod() {
    let playMod = this.data.playMod + 1
    if (playMod > SINGLE_CYCLE_MODE) {
      playMod = SEQ_MODE
    }
    this.setData ({
      playMode: playMod
    })
  },
  // 上一首
  prev() {
    app.currentIndex = this.getNextIndex(false)
    this._init()
  },
  // 下一首
  next() {
     app.currentIndex = this.getNextIndex(true)
     this._init()
  },
  // 获取不同播放模式下的下一曲索引
  _formatTime:function (interval) {
     interval = interval | 0
     const minute = interval / 60 | 0
     const second = this.pad(interval % 60)
    // 加1
     return `${minute}:${second}`
  },
  // 秒前面补0
  _pad(num, n = 2) {
    let len = num.toString().length
    while(len < n) {
      num = '0' + num
      len++
    }
    return num;
  },
    // 获取歌词
    _getLyric: function (currentSong) {
      const _this = this
      this._getBackPlayfileName().then((res) => {
        const nowMid = res.ret.split('.')[0].replace('C400', '')
        if (!(nowMid === currentSong.mid)) {
          if (this.data.currentLyric) {
            //this.data.currentLyric.stop && this.data.currentLyric.stop()
          }
          _this._getLyricAction(currentSong)
        }
      }).catch(() => {
        _this._getLyricAction(currentSong)
      })
    },
  
    // 获取处理歌词
    _getLyricAction: function (currentSong) {
      song.getLyric(currentSong.mid).then((res) => {
        if (res.data.showapi_res_body.ret_code == 0) {
          const lyric = this._normalizeLyric(res.data.showapi_res_body.lyric)
          const currentLyric = new Lyric(lyric)
          this.setData({
            currentLyric: currentLyric
          })
        } else {
          this.setData({
            currentLyric: null,
            currentText: ''
          })
        }
      })
    },
    // 去掉歌词中的转义字符
    _normalizeLyric: function (lyric) {
      return lyric.replace(/&#58;/g, ':').replace(/&#10;/g, '\n').replace(/&#46;/g, '.').replace(/&#32;/g, ' ').replace(/&#45;/g, '-').replace(/&#40;/g, '(').replace(/&#41;/g, ')')
    },
    // 歌词滚动回调函数
    handleLyric: function (currentTime) {
      let lines = [{time: 0, txt: ''}], lyric = this.data.currentLyric, lineNum
      lines = lines.concat(lyric.lines)
      for (let i = 0; i < lines.length; i++) {
        if (i < lines.length - 1) {
          let time1 = lines[i].time, time2 = lines[i + 1].time
          if (currentTime > time1 && currentTime < time2) {
            lineNum = i - 1
            break;
          }
        } else {
          lineNum = lines.length - 2
        }
      }
      this.setData({
        currentLineNum: lineNum,
        currentText: lines[lineNum + 1] && lines[lineNum + 1].txt
      })
  
      let toLineNum = lineNum - 5
      if (lineNum > 5 && toLineNum != this.data.toLineNum) {
        this.setData({
          toLineNum: toLineNum
        })
      }
    },
    _formatTime: function (interval) {
      interval = interval | 0
      const minute = interval / 60 | 0
      const second = this._pad(interval % 60)
      return `${minute}:${second}`
    },
    getNextIndex: function (nextFlag) {
      let ret,
          currentIndex = app.currentIndex,
          mod = this.data.playMod,
          len = this.data.songslist.length
      if (mod === RANDOM_MOD) {
        ret = util.randomNum(len)
      } else {
        if (nextFlag) {
          ret = currentIndex + 1 == len ? 0 : currentIndex + 1
        } else {
          ret = currentIndex - 1 < 0 ? len - 1 : currentIndex - 1
        }
      }
      return ret
    },
    togglePlaying: function () {
      wx.getBackgroundAudioPlayerState({
        success: function (res) {
          var status = res.status
          if (status == 1) {
            wx.pauseBackgroundAudio()
          } else {
            wx.playBackgroundAudio()
          }
        }
      })
    },
    openList: function () {
      if (!this.data.songslist.length) {
        return
      }
      this.setData({
        translateCls: 'uptranslate'
      })
    },
    close: function () {
      this.setData({
        translateCls: 'downtranslate'
      })
    },
    playthis: function (e) {
      const index = e.currentTarget.dataset.index
      app.currentIndex = index
      this._init()
      this.close()
    },
    changeDot: function (e) {
      this.setData({
        currentDot: e.detail.current
      })
    }
  // 获取音乐id
  // 获取播放地址
  // 创建播放器
  // 获取歌词并处理
  // 获取不同播放模式下的下一曲索引

})